﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FieldDevice;
using FieldLib;
namespace FieldDevice.UI
{
    public partial class DeviceListForm : Form
    {
        public DeviceListForm()
        {
            InitializeComponent();
        }

        System.Windows.Forms.Timer ListTime = new Timer();

        private void startWorkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DeviceManager.GlobalDeviceManager == null)
            {
                MessageBox.Show(this, "请先点击'Begin'菜单!");
                return;
            }
            try
            {
                Startup();
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }

        private void toolStripMenuItemBegin_Click(object sender, EventArgs e)
        {
            SystemConfig sysconfig = SystemConfig.GlobalSystemConfig;
            DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation =
                (sysconfig.DeviceEmulation == SystemConfig.EnumDeviceEmulation.Emulation);
          if (!DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
            {
                try
                {
                    DeviceManager.GlobalDeviceManager.InitializeCommHub();
                    DeviceManager.GlobalDeviceManager.OpenCommHub();
                    //DeviceManager.GlobalDeviceManager.ModBusIO.Channel = new FieldLib.CommChannel();//new ChannelBrighe(this.dcnproxyControl1);
                    //DeviceManager.GlobalDeviceManager.ModBusIO.Channel.Open();
                }
                catch (Exception exp)
                {
                    MessageBox.Show(this, exp.ToString());
                }
            }
            try
            {

                DeviceManager.GlobalDeviceManager.CreateDevices();

                if(DeviceManager.GlobalDeviceManager.UIManager ==null)
                    DeviceManager.GlobalDeviceManager.UIManager = new FieldDevice.UI.UIManager(this);
                if (SystemConfig.GlobalSystemConfig.DeviceEmulation == SystemConfig.EnumDeviceEmulation.Emulation)
                {
                    //这里必须跟DeviceManager.CreateDevices()一致

                    FieldDevice.UI.DeviceForm form1 = new FieldDevice.UI.DeviceForm(DeviceManager.GlobalDeviceManager.GetDevice(1));
                    //this.DeviceList.Items.Add(InitItem(this.form1));
                    form1.Show();

                    FieldDevice.UI.DeviceForm form3 = new FieldDevice.UI.DeviceForm(DeviceManager.GlobalDeviceManager.GetDevice(8));
                    //this.DeviceList.Items.Add(InitItem(this.form3));
                    form3.Show();
                }
                this.DeviceList.Load();
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }

        void Startup()
        {
            SystemConfig sysconfig = SystemConfig.GlobalSystemConfig;
            if (sysconfig.ScheduleManner == SystemConfig.EnumScheduleManner.BackThread)
            {
                DeviceManager.GlobalDeviceManager.StarupBackThreads();
            }
        }

        public ListViewItem InitItem(FieldDevice.UI.DeviceForm form)
        {
            //foreach (Device device in DeviceManager.GlobalDeviceManager.Devices.Values)
            //{
            //    ListViewItem item=this.DeviceList.
            //}
            ListViewItem listView = new ListViewItem(form.Device1.ModbusAddress.ToString());
            switch (form.Device1.ModbusAddress)
            {
                case 7:
                    listView.SubItems.Add("ACR330");
                    break;
                case 8:
                    listView.SubItems.Add("PM800");
                    break;
                default:
                    break;
            }
            listView.SubItems.Add("正常");
            listView.SubItems.Add(form.ShowUA);
            listView.SubItems.Add(form.ShowUB);
            listView.SubItems.Add(form.ShowUC);
            return listView;
        }



        private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FieldLib.UI.SystemConfigDialog dlg = new FieldLib.UI.SystemConfigDialog(SystemConfig.GlobalSystemConfig);
            dlg.ShowDialog(this);
        }


        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeviceManager.GlobalDeviceManager.Finallize();
        }

        private void DeviceListForm_Load(object sender, EventArgs e)
        {
            //this.DeviceList.SetGroupCollapse(FieldLib.UI.GroupState.COLLAPSIBLE);
            try
            {
                if (DeviceManager.GlobalDeviceManager.IsCommInitialized)
                    this.DeviceList.Load();
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }

 
    }
}
